﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.DanhHieuKhenThuongService;
namespace eTMS.Controllers{
public class DanhHieuKhenThuongControllerBase : CommonController{
	public DanhHieuKhenThuongControllerBase()
		: base()
	{	
		this.dao = new DanhHieuKhenThuongServiceClient();
	}
	~DanhHieuKhenThuongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private DanhHieuKhenThuongEntity oDanhHieuKhenThuong;	
	public DanhHieuKhenThuongEntity DanhHieuKhenThuongObject{
		get{
			return oDanhHieuKhenThuong;
		}
		set{
			oDanhHieuKhenThuong=value;
		}
	}
	protected DanhHieuKhenThuongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSDanhHieuKhenThuong(false);			
			return _Table;
		}
	}
	public void LoadAllDSDanhHieuKhenThuong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oDanhHieuKhenThuong==null)return false;
		bool result=true;
		try{
			oDanhHieuKhenThuong = dao.SaveRet(oDanhHieuKhenThuong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListDanhHieuKhenThuong.Add(oDanhHieuKhenThuong);
		}
		return result;
	}
	public bool Update(){
		if(oDanhHieuKhenThuong==null)return false;
		return dao.Update(oDanhHieuKhenThuong);
	}
	public bool Delete(){
		if(oDanhHieuKhenThuong==null)return false;
		bool retvalue = dao.Delete(oDanhHieuKhenThuong);
		if(retvalue){			
			_ListDanhHieuKhenThuong.Remove(oDanhHieuKhenThuong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 DanhHieuKhenThuong_ID){
		bool retvalue = dao.DeleteByID(DanhHieuKhenThuong_ID);		
		if(retvalue){
			DanhHieuKhenThuongEntity oDeleted=FindDanhHieuKhenThuong(DanhHieuKhenThuong_ID);
			if(oDeleted!=null){
				_ListDanhHieuKhenThuong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oDanhHieuKhenThuong = new DanhHieuKhenThuongEntity();
	}
	//Check if exists MaDanhHieu
	public bool CheckExistsMaDanhHieu(object value){	
		return dao.SelectOneByMaDanhHieu(value.ToString()).MaDanhHieu!=null;
    }    
    //Copy data from data row in grid to object
    public DanhHieuKhenThuongEntity MapDataRowToObject(DataRow row){
		DanhHieuKhenThuongEntity o = new DanhHieuKhenThuongEntity();
		 o.MaDanhHieu=Convert.ToString(row["MaDanhHieu"]);
		 o.TenDanhHieu=Convert.ToString(row["TenDanhHieu"]);
		 o.MoTaDanhHieu=Convert.ToString(row["MoTaDanhHieu"]);
		 if(!row.IsNull("DanhHieuKhenThuong_ID"))
		    o.DanhHieuKhenThuong_ID=(Int32)(row["DanhHieuKhenThuong_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(DanhHieuKhenThuongEntity o,DataRow row){
		row["MaDanhHieu"]=o.MaDanhHieu;
		row["TenDanhHieu"]=o.TenDanhHieu;
		row["MoTaDanhHieu"]=o.MoTaDanhHieu;
		row["DanhHieuKhenThuong_ID"]=o.DanhHieuKhenThuong_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.DanhHieuKhenThuongObject,row);
    }
	public void LoadObject(Int32 DanhHieuKhenThuong_ID){
		DanhHieuKhenThuongEntity oLoad=FindDanhHieuKhenThuong(DanhHieuKhenThuong_ID);
		if(oLoad==null){
			oDanhHieuKhenThuong=this.LoadDanhHieuKhenThuong(DanhHieuKhenThuong_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oDanhHieuKhenThuong.IsNullDanhHieuKhenThuong_ID;
			if(!bNotIsNull){
				this.ListDanhHieuKhenThuong.Add(oDanhHieuKhenThuong);
			}
		}
		else{
			oDanhHieuKhenThuong=oLoad;
		}
	}
	#region CachedList DanhHieuKhenThuong
	private static List<DanhHieuKhenThuongEntity> _ListDanhHieuKhenThuong=new List<DanhHieuKhenThuongEntity>();
    public List<DanhHieuKhenThuongEntity>ListDanhHieuKhenThuong{
		get{
			return _ListDanhHieuKhenThuong;
		}
	}
	public static List<DanhHieuKhenThuongEntity>ListDanhHieuKhenThuongStatic{
		get{
			return _ListDanhHieuKhenThuong;
		}
	}
	public DanhHieuKhenThuongEntity FindDanhHieuKhenThuong(Int32 DanhHieuKhenThuong_ID){
		DanhHieuKhenThuongEntity oResult=this.ListDanhHieuKhenThuong.Find(delegate(DanhHieuKhenThuongEntity oFind){
			bool result=true; 
			result&=oFind.DanhHieuKhenThuong_ID.Equals(DanhHieuKhenThuong_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllDanhHieuKhenThuong(bool clearCache){
		if(_ListDanhHieuKhenThuong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		DanhHieuKhenThuongServiceClient dao = new DanhHieuKhenThuongServiceClient();
		_ListDanhHieuKhenThuong=new List<DanhHieuKhenThuongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public DanhHieuKhenThuongEntity LoadDanhHieuKhenThuong(Int32 DanhHieuKhenThuong_ID){			
		DanhHieuKhenThuongEntity o=dao.SelectOne(DanhHieuKhenThuong_ID);		
		return o;
	}
	#endregion
}
}
